package com.zmn.uac.client;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.tapi.common.alipay.AlipaySystemOauthToken;
import com.zmn.tapi.dubbo.DubboConsts;
import com.zmn.tapi.dubbo.interfaces.aliapy.AlipayRemoteService;
import com.zmn.uac.UacException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @title: 支付宝Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 10:41
 */
@Slf4j
@Component
public class AlipayClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    AlipayRemoteService alipayRemoteService;


    /**
     * 使用授权码换取令牌
     * @param appId 应用ID
     * @param privateKey 用户私钥
     * @param alipayPublicKey 支付宝公钥
     * @param authCode 授权码；临时的用户授权凭证，获取后应及时换取下面提到的授权令牌
     * @param refreshToken 刷新令牌；用于在授权令牌过期后刷新重新获取新的授权令牌，刷新令牌也有有效期
     * @return AlipaySystemOauthToken
     */
    public AlipaySystemOauthToken getAlipaySystemOauthToken(String appId, String privateKey, String alipayPublicKey, String authCode, String refreshToken){
        log.info("获取支付宝小程序调用请求 appId:{},privateKey:{},alipayPublicKey:{},authCode:{},refreshToken:{}", appId, privateKey, alipayPublicKey, authCode,refreshToken);
        ResponseDTO<AlipaySystemOauthToken> tokenResponse = alipayRemoteService.getAlipaySystemOauthToken(appId, privateKey, alipayPublicKey, authCode, refreshToken);
        if(!tokenResponse.isSuccess()){
            log.error("获取支付宝小程序Token请求失败, msg: {}",tokenResponse.getMessage());
            throw new UacException(tokenResponse.getMessage());
        }
        AlipaySystemOauthToken token = tokenResponse.getData();
        log.info("获取支付宝小程序Token调用返回response:{}", token);

        return token;
    }
}
